package com.tywj.panda.home.details

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DimenRes
import com.tywj.panda.customer.common.entity.HomeLineDetailsEntity
import com.tywj.panda.customer.common.widget.recyclerview.MyBaseAdapter
import com.tywj.panda.home.R
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.widget.recycleview.adapter.BaseViewHolder

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/25
 *     desc   : 线路详情适配器
 *     version: 2.0.0
 * </pre>
 */
class HomeLineDetailsAdapter :
    MyBaseAdapter<HomeLineDetailsEntity.Station>(R.layout.home_line_recyclerview_item_details) {

    //局部刷新
    companion object {
        const val UPDATE_TYPE_STATION_STATUS = 1
        const val UPDATE_TYPE_SET_START_OR_END_STATUS = 2
    }


    override fun convert(helper: BaseViewHolder, item: HomeLineDetailsEntity.Station) {
        super.convert(helper, item)

        helper.setText(R.id.tvTime, item.timeAdd)
        helper.setText(R.id.tvAddress, item.name)

        helper.addOnClickListener(R.id.tvSetStartPoint, R.id.tvSetEndPoint, R.id.tvAddress)

        updateStatus(helper, item)
        updateStationColor(helper, item)
    }

    private fun setImageViewStatus(view: ImageView, @DimenRes size: Int) {
        val lp = view.layoutParams
        lp.width = ResourcesUtil.getDimensionPixelSize(size)
        lp.height = ResourcesUtil.getDimensionPixelSize(size)
        view.layoutParams = lp
    }

    override fun handleLocalRefresh(
        helper: BaseViewHolder,
        item: HomeLineDetailsEntity.Station,
        payloads: MutableList<Any>
    ) {
        super.handleLocalRefresh(helper, item, payloads)

        //待优化
        when (payloads.first().toString().toInt()) {
            UPDATE_TYPE_STATION_STATUS -> {
                updateStatus(helper, item)
            }
            UPDATE_TYPE_SET_START_OR_END_STATUS -> updateStationColor(helper, item)
        }
    }

    //展开或关闭状态更新
    private fun updateStatus(helper: BaseViewHolder, item: HomeLineDetailsEntity.Station) {
        if (item.isExpanded) {
            helper.setGone(R.id.viewBottomBackground, true)
            helper.setGone(R.id.tvSetStartPoint, true)
            helper.setGone(R.id.tvSetEndPoint, true)
        } else {
            helper.setGone(R.id.viewBottomBackground, false)
            helper.setGone(R.id.tvSetStartPoint, false)
            helper.setGone(R.id.tvSetEndPoint, false)
        }
    }

    //设置为起点或终点的颜色变化
    private fun updateStationColor(helper: BaseViewHolder, item: HomeLineDetailsEntity.Station) {

        val imageViewStatus = helper.getView<ImageView>(R.id.ivStationStatus)
        val tvStation = helper.getView<TextView>(R.id.tvAddress)
        val viewBg = helper.getView<View>(R.id.viewTopBackground)

        if (item.isTagStartStation || item.isTagEndStation) {

            imageViewStatus.background = ResourcesUtil.getDrawable(R.drawable.home_line_details_selected)
            setImageViewStatus(imageViewStatus, R.dimen.dp_12)
            tvStation.setTextColor(ResourcesUtil.getColor(R.color.common_yellow_FD))

        } else {
            tvStation.setTextColor(ResourcesUtil.getColor(R.color.common_black_33))

            if (helper.adapterPosition == 0) {
                viewBg.visibility = View.INVISIBLE
                imageViewStatus.background = ResourcesUtil.getDrawable(R.drawable.home_line_detail_start)
                setImageViewStatus(imageViewStatus, R.dimen.dp_12)
            } else {
                //显示顶部的背景
                helper.setGone(R.id.viewTopBackground, true)

                //尾部
                if (helper.adapterPosition == this@HomeLineDetailsAdapter.data.size - 1 + this@HomeLineDetailsAdapter.headerLayoutCount) {

                    imageViewStatus.background = ResourcesUtil.getDrawable(R.drawable.home_line_detail_start)
                    helper.setGone(R.id.viewCenterBackground, false)
                    setImageViewStatus(imageViewStatus, R.dimen.dp_12)
                } else {
                    //中间正常显示
                    imageViewStatus.background = ResourcesUtil.getDrawable(R.drawable.home_line_details_item_tag)

                    setImageViewStatus(imageViewStatus, R.dimen.dp_6)
                }
            }
        }
    }
}